<?php

/**
 * this class provides function for creating an assignbox
 * 
 * please make sure, that you have included the assignbox.js from public/backend/js
 * this file handles the functionality if the user clicks on a arrow button
 * 
 * each box will have his own id
 * the ids are
 *     left:    <<custom_id_text>>_ASSIGNBOX_LEFT
 *     right:   <<custom_id_text>>_ASSIGNBOX_RIGHT
 * @author		ungerboeck simon <su@militaerversand.at>
 *
 */
class Assignbox {
    private $id = null;
    private $leftHeader = null;
    private $rightHeader = null;
    private $width = null; //width of one of the boxes
    private $rowCount = null; //min. rows 2. rows which are visible in each box
    private $leftContent = null;    //array of values in this form array(<<key>> => <<value>>, ...)
    private $rightContent = null;
    private $leftEvents = null;
    private $rightEvents = null;
    private $disabled = null;


    /************************************************************************
     ************************** PUBLIC FUNCTIONS ****************************
     ************************************************************************/
    public function __construct($id = null) {
        $id = (isset($id) ? $id : uniqid());
        $this->id = $id;
        $this->rowCount = 7;
        $this->width = 130;
        $this->leftHeader = '&nbsp;';
        $this->rightHeader = '&nbsp;';
        $this->leftContent = array();
        $this->rightContent = array();
        $this->rightEvents = array();
        $this->leftEvents = array();
        $this->disabled = false;
    }


    /**
     * creates the box in html format
     * @return        string
     */
    public function create() {
        $output = '<div class="formStyle">';
        $output .= '<table>';
        $output .= $this->getHeaders();
        $output .= $this->getBoxesAndArrows();
        $output .= '</table>';
        $output .= '</div>';
        return $output;
    }


    /**
     * converts the box in html string
     * @return        string
     */
    public function __toString() {
        return $this->create();
    }
    
    
    /**
     * add an entry in the left box
     * @param         string         $key
     * @param         string         $value
     */
    public function addLeftEntry($key, $value){
        if (isset($key) and isset($value))
            $this->leftContent[$key] = $value;
    }
    
    
    /**
     * add an entry in the right box
     * @param         string         $key
     * @param         string         $value
     */
    public function addRightEntry($key, $value){
        if (isset($key) and isset($value))
            $this->rightContent[$key] = $value;
    }
    
    
    /**
     * adds an event to left box
     * @param 		string		 $eventName
     * @param 		string		 $function
     */
    public function addLeftEvent($eventName, $function){
    	$this->leftEvents[$eventName] = $function;
    }
    
    
    /**
     * adds an event to right box
     * @param 		string		 $eventName
     * @param 		string		 $function
     */
    public function addRightEvent($eventName, $function){
    	$this->rightEvents[$eventName] = $function;
    }
    
    
    public function disable(){
    	$this->disabled = true;
    }


    /************************************************************************
     **************************** GETTER / SETTER ***************************
     ************************************************************************/

    /**
     * sets the id of current assignbox
     * @param         string         $id
     */
    public function setId($id) {
        $this->id = $id;
    }


    /**
     * sets the header for the left box
     * @param         string         $header
     */
    public function setLeftHeader($header) {
        $this->leftHeader = $header;
    }


    /**
     * sets the header of the right box
     * @param         string         $header
     */
    public function setRightHeader($header) {
        $this->rightHeader = $header;
    }


    /**
     * sets the with of each box 
     * @param         int         $width    width for each of the boxes
     * @throws        Exception
     */
    public function setWidth($width) {
        if (is_numeric($width))
            $this->width = $width;
        else
            throw new Exception('your given width of assignbox is not a numeric value!');
    }


    /**
     * sets the rows of the boxes
     * @param         int         $rows
     * @throws        Exception
     */
    public function setRowCount($rowCount) {
        if (is_numeric($rowCount)) {
            $rowCount = ($rowCount < 5) ? 5 : $rowCount; //if is lower then 5, min rows are 5
            $this->rowCount = $rowCount;
        } else
            throw new Exception('your given count of rows is not a numeric value!');
    }


    /**
     * returns the id base for the boxes
     * @return         string
     */
    public function getId() {
        return $this->id;
    }


    /**
     * returns the header of the left box
     * @return         string
     */
    public function getLeftHeader() {
        return $this->leftHeader;
    }


    /**
     * returns the header of the right box
     * @return         string
     */ 
    public function getRightHeader() {
        return $this->rightHeader;
    }


    /**
     * returns the with for one of the boxes
     * @return        int        
     */
    public function getWidth() {
        return $this->width;
    }


    /**
     * returns the row count for one box
     * @return         int
     */
    public function getRowCount() {
        return $this->rowCount;
    }


    /************************************************************************
     ************************** PRIVATE FUNCTIONS ***************************
     ************************************************************************/

    /**
     * returns the header row
     * @return        string        $output
     */
    private function getHeaders() {
        $output = '<thead>';
        $output .= '<tr>';
        $output .= '<th style="text-align: center; color: #373737">' . $this->leftHeader . '</th>';
        $output .= '<th>&nbsp;</th>';
        $output .= '<th style="text-align: center; color: #373737">' . $this->rightHeader . '</th>';
        $output .= '</tr>';
        $output .= '</thead>';
        return $output;
    }
    
    
    /**
     * returns the row for the boxes and the arrows
     * @return        string        $output
     */
    private function getBoxesAndArrows(){
        $output = '<tbody>';
        $output .= '<tr>';
        $output .= '<td>' . $this->createBox('L') . '</td>';
        $output .= '<td> ' . $this->createIcons() . '</td>';
        $output .= '<td>' . $this->createBox('R') . '</td>';
        $output .= '</tr>';
        $output .= '</tbody>';
        return $output;
    }
    
    
    /**
     * creates the icons for moving selected entries
     * @return         string        $output
     */
    private function createIcons(){
        $output = '<img ' . ((!$this->disabled) ? 'onclick="moveAllRight(\'' . $this->id . '\'); "' : '') . 'src="' . ADMIN_IMAGES . 'move_right_double.png" alt="move_right_double.png" style="cursor: pointer" title="alles nach rechts" /><br />';
        $output .= '<img ' . ((!$this->disabled) ? 'onclick="moveRight(\'' . $this->id . '\'); "' : '') . 'src="' . ADMIN_IMAGES . 'move_right.png" alt="move_right.png" style="cursor: pointer" title="ausgew&auml;hltes nach rechts" /><br />';
        $output .= '<img ' . ((!$this->disabled) ? 'onclick="moveLeft(\'' . $this->id . '\'); "' : '') . 'src="' . ADMIN_IMAGES . 'move_left.png" alt="move_left.png" style="cursor: pointer" title="ausgew&auml;hltes nach links" /><br />';
        $output .= '<img ' . ((!$this->disabled) ? 'onclick="moveAllLeft(\'' . $this->id . '\'); "' : '') . 'src="' . ADMIN_IMAGES . 'move_left_double.png" alt="move_left_double.png" style="cursor: pointer" title="alles nach links" /><br />';
        return $output;
    }
    
    
    /**
     * creates one box
     * @param unknown $box
     */
    private function createBox($box){
        $postfix = $this->getPostfix($box);
        $id = $this->id . $postfix;
        $options = $this->getOptions($id, $box);
        $content = $this->getContent($box);
        return \Form::select($id, $content, array(), $options);
    }
    
    
    /**
     * returns the postfix for given box
     * @param         string         $box
     * @return        string         $postfix
     */
    private function getPostfix($box){
        $postfix = null;
        switch(strtoupper($box)){
            case 'L':
                $postfix = '_ASSIGNBOX_LEFT';
                break;
            case 'R':
                $postfix = '_ASSIGNBOX_RIGHT';
                break;
        }
        return $postfix;
    }
    
    
    /**
     * returns the options for one box
     * @return        array        $options
     */
    private function getOptions($id, $box){
        $options = array();
        $options['id'] = $id;
        $options['size'] = $this->rowCount;
        $options['style'] = 'width: ' . $this->width . 'px';
        $options['multiple'] = 'multiple';
        
        if ($this->disabled)
        	$options['disabled'] = 'disabled';
        
        $events = (strtoupper($box) == 'L') ? $this->leftEvents : $this->rightEvents;
        foreach($events as $event => $function)
        	$options[$event] = $function;
        
        return $options;
    }
    
    
    /**
     * returns the content for one box
     * @param         string         $box
     * @return        array          $content
     */
    public function getContent($box){
        $content = null;
        switch(strtoupper($box)){
            case 'L':
                $content = $this->leftContent;
                break;
            case 'R':
                $content = $this->rightContent;
                break;
        }
        return $content;
    }

}

?>